package 力扣日常刷题.木23一月.第19天319;

public class Demo97在排序数组中查找数字1 {

    public int search(int[] nums,int target) {
        if(nums.length == 0) {
            return 0;
        }
        int l = 0, r = nums.length - 1;
        //find left
        while(l < r) {
            int mid = (l + r) >> 1;
            if(nums[mid] >= target) {
                r = mid;
            }else{
                l = mid + 1;
            }
        }
        if(nums[l] != target){
            return 0;
        }
        int k = l;
        r =  nums.length -1;
        while(l < r) {
            int mid = (l + r) >> 1;
            if(nums[mid] <= target){
                l = mid;
            }else {
                r = mid - 1;
            }
        }
        return r - k + 1;

    }

}
